<template>
  <div class="box_cont">
    <!-- <p>客户端安全策略</p>
    <div class="kehuan">
      <el-form label-width="140px">
        <div class="awas">
          <el-form-item label="客户端锁屏时间">
            <el-input-number v-model="ClientLockTime" controls-position="right" @change="handleChange" :min="1" :max="10"></el-input-number>
            <span>分钟</span>
          </el-form-item>
        </div>
        <div class="awas">
          <el-form-item label="拨key设置">
            <el-select v-model="RemoveKey" clearable placeholder="请选择">
              <el-option v-for="item in keyoptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="awas" style="margin-top: 30px">
          <el-form-item>
            <el-button type="primary" @click="baoscus">保存</el-button>
            <el-button type="primary">取消</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div> -->
    <p>客户端安全策略/usbkey策略</p>
    <div class="usbkey">
      <el-form label-width="140px" :model="forinsm">
        <div class="awas">
          <el-form-item label="客户端锁屏时间">
            <el-input-number disabled v-model="forinsm.ClientLockTime" controls-position="right" @change="handleChange" :min="0" :max="30"></el-input-number>
            <span>分钟</span>
          </el-form-item>
        </div>
        <div class="awas daushu">
          <el-form-item label="拨key设置">
            <el-select disabled v-model="forinsm.RemoveKey" clearable placeholder="请选择">
              <el-option v-for="item in keyoptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="awas">
          <el-form-item label="PIN码复杂度">
            <el-checkbox-group disabled v-model="forinsm.Complex">
              <el-checkbox label="1">字母</el-checkbox>
              <el-checkbox label="2">数字</el-checkbox>
              <el-checkbox label="4">特殊字符</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </div>
        <div class="awas">
          <el-form-item label="PIN码长度">
            <div class="miahcad">
              <el-input disabled v-model="forinsm.PINLengthS" placeholder="请输入PIN码最小长度"></el-input>
              <span style="margin: 0 10px">-</span>
              <el-input disabled v-model="forinsm.PINLengthE" placeholder="请输入PIN码最大长度"></el-input>
            </div>
          </el-form-item>
        </div>
        <div class="awas">
          <el-form-item label="PIN码过期时间">
            <el-input-number disabled v-model="forinsm.PinOverDue" controls-position="right" @change="handleChange" :min="0" :max="7"></el-input-number>
            <span>天</span>
            <span style="margin-left: 20px">1~7天</span>
          </el-form-item>
        </div>
        <!-- <div class="awas">
          <el-form-item label="PIN码最大尝试次数">
            <el-input-number v-model="forinsm.leftRetry" controls-position="right" @change="handleChange" :min="1" :max="10"></el-input-number>
            <span>次</span>
          </el-form-item>
        </div> -->

        <!-- <div class="awas" style="margin-top: 30px">
          <el-form-item>
            <el-button type="primary" @click="baoscus">保存</el-button>
            <el-button type="primary">取消</el-button>
          </el-form-item>
        </div> -->
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "policyManagement",
  data() {
    return {
      accountNameOnline: '',
      RemoveKey: "",
      keyoptions: [
        {
          value: "0",
          label: "锁屏",
        },
        {
          value: "1",
          label: "注销",
        },
      ],
      PinComplex: [],
      PinLengthMin: "1",
      PinLengthMax: "1",
      PinExpireTime: "1",
      PinTryNum: "1",
      forinsm: {
        ClientLockTime: "0",
        KeyType: "",
        RemoveKey: "0",
        Complex: [],
        PINLengthS: "",
        PINLengthE: "",
        PinOverDue: "",
        leftRetry: "",
      },
      timestamp: "", //时间戳
      userName: window.sessionStorage.getItem("userName"),
    };
  },
  created() {
    this.timestamp = new Date().getTime();
    // this.huolist();
    this.getUserData()
  },
  methods: {
    handleChange(value) {
      console.log(value);
    },
    // 获取
    huolist() {
      this.$get("/aqdl/policy/getcontext?type=53").then((res) => {
        console.log(res);
        this.forinsm = res.data.data.policy[0].context.Root;
        let data = res.data.data.policy[0].context.Root;
        this.forinsm.ClientLockTime = data.ClientLockTime
        console.log(this.forinsm);
        if (data.Complex == "3") {
          this.forinsm.Complex = ["1", "2"];
        } else if (data.Complex == "5") {
          this.forinsm.Complex = ["1", "4"];
        } else if (data.Complex == "6") {
          this.forinsm.Complex = ["2", "4"];
        } else if (data.Complex == "7") {
          this.forinsm.Complex = ["1", "2", "4"];
        } else if (data.Complex == "1") {
          this.forinsm.Complex = ["1"];
        } else if (data.Complex == "2") {
          this.forinsm.Complex = ["2"];
        } else if (data.Complex == "4") {
          this.forinsm.Complex = ["4"];
        }
      });
    },
    baoscus() {
      this.$confirm("确认保存？", "提示", {
        confirmButtonText: "确 定",
        cancelButtonText: "取 消",
        type: "warning",
      })
        .then(() => {
          console.log();
          let data = {
              "policy": [
                {
                    "state": "1",
                    "context": {
                        "Root": {
                            "KeyType": this.forinsm.KeyType,
                            "Complex": eval(this.forinsm.Complex.join("+")).toString(),
                            "PINLengthS": this.forinsm.PINLengthS,
                            "PINLengthE": this.forinsm.PINLengthE,
                            "PinOverDue": this.forinsm.PinOverDue.toString(),
                            "RemoveKey": this.forinsm.RemoveKey,
                            "ClientLockTime": (parseInt(this.forinsm.ClientLockTime)* 60).toString(),
                            "HeartbeatTime": "30",
                            "VerifyPasswd": "1"
                        }
                    }
                }
            ]
          };
          this.$postForJson("/aqdl/policy/setcontext", data).then((res) => {
            console.log(res);
            if (res.data.errorcode == "0") {
              this.$message({
                type: "success",
                message: "保存成功！",
              });
              this.setlogrlist("326", res.data.errorcode, "成功");
            } else {
              this.$message({
                type: "error",
                message: "保存失败！",
              });
              this.setlogrlist("326", res.data.errorcode, "失败");
            }
          });
        })
        .catch((_) => {});
    },
    //获取账户
    getUserData() {
      this.$get("/aqdl/user/getloginuser").then((res) => {
        console.log(res,293);
        this.accountNameOnline = res.data.data
        this.huolist();
      });
    },
    //10位时间戳转换
    getLocalTime(nS) {
      let time = new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ').replace(/\//g, '-');
      // 找出出现空格的位置
      let space_position = time.indexOf(' ');
      // 截取并返回数据
      return time.slice(0,space_position);
    },
    // 记录操作日志接口
    setlogrlist(tyid, sults, redusl) {
      let datess = Date.now()
      datess= (datess+'').substring(0,10)
      console.log(new Date(this.getLocalTime(datess)+''))
      let eTimes = new Date(this.getLocalTime(datess)+'').getTime();
      eTimes = parseInt(eTimes)/1000
      console.log(eTimes,"Timestamp")
      let daJson = {
        Root: {
          LogType: "50",
          LogRecord: {
            "Timestamp": eTimes+'',
            // EmpID: "1",
            // EmpName: "test",
            Type: tyid,
            // KeySerial: "2DD66DF1BEDB7C895C",
            // KeyType: "0",
            Account: this.accountNameOnline,
            Role: 'admin',
            Result: sults+'',
            ResultDesc: redusl,
          },
        },
      };
      let daJsons = {
        Root: {
          LogType: "50",
          LogRecord: {
            "Timestamp": eTimes+'',
            // EmpID: "1",
            // EmpName: "test",
            Type: tyid,
            // KeySerial: "2DD66DF1BEDB7C895C",
            // KeyType: "0",
            Account: this.accountNameOnline,
            Role: '0',
            Result: sults+'',
            ResultDesc: redusl,
          },
        },
      };
      this.$postForJson("/aqdl/auditlog/setlogrecord", daJson).then((res) => {
        console.log(res);
      });
      this.$postForJson("/aqdl/auditlog/setlogrecord", daJsons).then((res) => {
        console.log(res);
      });
    },
  },
};
</script>

<style scoped>
.box_cont {
  display: flex;
  flex-direction: column;
  padding: 20px;
}
.kehuan {
  width: 100%;
  height: 35%;
  border: 1px solid #ccc;
  margin-bottom: 20px;
  padding: 20px 0 0 10px;
}
.usbkey {
  width: 100%;
  height: 10%;
  border: 1px solid #ccc;
  padding: 20px 0 0 10px;
  margin-top: 10px;
}
.awas {
  margin-bottom: 18px;
}
.daushu {
  width: 332px;
}
.usbkey .miahcad {
  width: 326px;
  display: flex;
}
.el-input-number--mini {
  width: 150px;
}
</style>